const app = getApp();
import {addAddress,updataAddress} from '../../../api/address'
import schema from 'async-validator'
import QQMapWX from '../../../libs/qqmap-wx-jssdk.js'
Page({
  // 页面的初始数据
  data: {
    name:"",
    phone:"",
    provinceName:"",//省
    provinceCode:"",
    cityName:"",//市
    cityCode:"",
    districtName:"",//区
    districtCode:"",
    address:"",//详细地址
    fullAddress:"",//完整地址
    isDefault:false,//默认地址，1为默认地址
    qqmapwx:""
  },
  validator(params){
    const rules={
      name:[
        {required:true,message:"不能为空"},
        {min:2,max:3,message:"长度只能是2到3个字符"},
        {type:'string',message:"只能是字符串"},
      ],
      phone:[
       {required:true,message:"不能为空"},
      ],
      provinceName:[
       {required:true,message:"不能为空"},
       {type:'string',message:"只能是字符串"},
      ],
      cityName:[
       {required:true,message:"不能为空"},
       {type:'string',message:"只能是字符串"},
      ],
      districtName:[
       {required:true,message:"不能为空"},
       {type:'string',message:"只能是字符串"},
      ],
      address:[
       {required:true,message:"不能为空"},
       {type:'string',message:"只能是字符串"},
      ],
      isDefault:[
       {required:true,message:"不能为空"},
      ]
    }
    const validator= new schema(rules)
    return new Promise((resolve)=>{
     validator.validate(params,(error)=>{
      if(error){
        wx.showModal({
          title: '错误',
          content:`${error[0].field}，${error[0].message}`,
        })
       resolve({valid:false})
      }else{
       resolve({valid:true})
      }
     })
    })
  },

  // 保存收货地址
  async saveAddrssForm(e) {
   const {provinceName,cityName,districtName,address,isDefault}=this.data
   const params={
     ...this.data,
     fullAddress:provinceName+""+cityName+""+districtName+""+address,
     isDefault:isDefault ==false?0:1
   }
  const {valid}= await this.validator(params)
   if(!valid) return
   if(app.onedit){
    // 修改地址，发送请求更新数据
    await updataAddress(params,app.addressid)
    wx.showToast({
      title: '修改成功',
    })
    app.onedit=false
   }else{
     // 新增地址，发送请求到服务器新增地址
    await addAddress(params)
   }
   wx.redirectTo({
    url: '/modules/settingmodule/pages/address/list/index',
   })
  },

  // 省市区选择
  onAddressChange(e) {
    this.setData({
      provinceName:e.detail.value[0],
      cityName:e.detail.value[1],
      districtName:e.detail.value[2]
    })
  },
  onShow(){
    if(app.onedit){
      this.setData({
        ...app.editaddressitem
      })
    }
  },
  // 定位
  async ondingwei(){
    const {latitude,longitude}= await wx.chooseLocation()
    this.qqmapwx.reverseGeocoder({
      location:{
        latitude,
        longitude
      },
      success:(res)=>{
        console.log(res);
        const {adcode}=res.result.ad_info
        const {province,city,district,street,street_number}=res.result.address_component
        this.setData({
          provinceName:province,//省
          provinceCode:adcode,
          cityName:city,//市
          cityCode:adcode,
          districtName:district,//区
          districtCode:adcode,
          address:street,//详细地址
          fullAddress:street+street_number,//完整地址
        })
      },
      fail:(error)=>{
        console.log(error);
        wx.showModal({
          title: '错误',
          content: error.message,
        })
      }
    })
  },
  onLoad(){
  this.qqmapwx=new QQMapWX({
    key:'YENBZ-DKNKC-TAZ2P-AM2AC-PQ7M2-IAFQL'
    })
  }
})
